const moment = require('moment')
const { sequelize } = require('../core/db')
const { Sequelize, Model } = require('sequelize')

class Category extends Model {}

Category.init(
  {
    id: {
      type: Sequelize.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    name: {
      type: Sequelize.STRING,
      allowNull: false,
      comment: '类别名称'
    },
    type: {
      type: Sequelize.STRING,
      allowNull: false,
      defaultValue: 'category',
      comment: '分类category/标签label'
    },
    created_at: {
      type: Sequelize.DATE,
      allowNull: false,
      get() {
        return moment(this.getDataValue('created_at')).format('YYYY-MM-DD H:m')
      }
    },
    updated_at: {
      type: Sequelize.DATE,
      allowNull: false,
      get() {
        return moment(this.getDataValue('updated_at')).format('YYYY-MM-DD H:m')
      }
    }
  },
  {
    sequelize,
    modelName: 'category',
    tableName: 'categorys'
  }
)

module.exports = Category
